package com.cloudkinto.controller.stock;


import com.cloudkinto.common.annotations.UserSessionRequired;
import com.cloudkinto.service.stock.vo.*;
import com.cloudkinto.service.storage.vo.PickupPreviewRes;
import io.swagger.annotations.*;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.cloudkinto.service.stock.StockPickUpService;

import com.cloudkinto.common.web.PageResult;
import com.cloudkinto.common.web.SingleResult;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.annotation.Validated;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.cloudkinto.base.BaseController;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zhujiale
 * @since 2022-12-23
 */
@RestController
@Api(tags = "拣货模块")
@RequestMapping("/api/admin/pickup")
public class StockPickUpController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(StockPickUpController.class);

    @Autowired
    private StockPickUpService service;

    @UserSessionRequired
    @GetMapping("/addInit")
    @ApiOperation(value = "新增初始化", notes = "新增初始化")
    public SingleResult addInit() {
        return service.addInit();
    }

    @UserSessionRequired
    @PostMapping("")
    @ApiOperation(value = "新增", notes = "新增")
    public SingleResult add(@RequestBody @Validated Object req) {
        Long userId = getCurrentUserId();
        Long companyId = getCompanyId();
        return SingleResult.success("新增成功", service.add(req, userId));
    }

    @UserSessionRequired
    @GetMapping("/updateInit/{id}")
    @ApiOperation(value = "编辑初始化", notes = "编辑初始化")
    public SingleResult updateInit(@PathVariable("id") Long id) {
        return service.updateInit(id);
    }

    @UserSessionRequired
    @PutMapping("/updateStatus")
    @ApiOperation(value = "修改拣货单状态", notes = "修改拣货单状态")
    public SingleResult update(@RequestBody @Validated UpdatePickUpReq req) {
        Long userId = getCurrentUserId();
        return SingleResult.success(service.update(req, userId), "");
    }

    @UserSessionRequired
    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据id删除", notes = "根据id删除")
    public SingleResult delete(@PathVariable("id") Long id) {
        Long userId = getCurrentUserId();
        return SingleResult.success("删除成功", service.delete(id, userId));
    }

    @UserSessionRequired
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查看详情", notes = "根据id查看详情")
    public SingleResult detail(@PathVariable("id") Long id) {
        return SingleResult.success("查询成功", service.detail(id));
    }

    @UserSessionRequired
    @GetMapping("/pageInit")
    @ApiOperation(value = "查询初始化", notes = "查询初始化")
    public SingleResult pageInit() {
        return service.pageInit();
    }

    @UserSessionRequired
    @GetMapping("/list")
    @ApiOperation(value = "查询", notes = "查询")
    public SingleResult list(HttpServletRequest request) {
        Long companyId = getCompanyId();

        Map<String, String> map = new HashMap<>();
        String keyword = request.getParameter("keyword");
        map.put("keyword", keyword);
        return SingleResult.success("查询成功", service.getList(map));
    }

    @UserSessionRequired
    @GetMapping("/listPage")
    @ApiOperation(value = "拣货查询（分页）", notes = "拣货查询（分页）")
    @ApiImplicitParams({@ApiImplicitParam(name = "current", value = "当前页"),
            @ApiImplicitParam(name = "status", value = "状态 0:待拣货 1:已拣货"),
            @ApiImplicitParam(name = "pickUpNumber", value = "拣货单号"),
            @ApiImplicitParam(name = "sortType", value = "排序规则 按sku:sku 按库位：rack"),
            @ApiImplicitParam(name = "storageId", value = "仓库id"),
            @ApiImplicitParam(name = "trackId", value = "快递公司id"),
            @ApiImplicitParam(name = "type", value = "fba还是一件代发"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小")})
    @ApiResponses(@ApiResponse(code = 200, message = "", response = StockPickUpRes.class))
    public PageResult listPage(HttpServletRequest request) {
        Long companyId = getCompanyId();

        Map<String, String> map = new HashMap<>();
        String pickUpNumber = request.getParameter("pickUpNumber");
        String status = request.getParameter("status");
        String storageId = request.getParameter("storageId");
        String sortType = request.getParameter("sortType");

        String trackId = request.getParameter("trackId");
        String type = request.getParameter("type");
        String current = request.getParameter("current");
        String size = request.getParameter("pageSize");
        if (StringUtils.isNotBlank(storageId)) {
            map.put("storageId", storageId);
        }
        if (StringUtils.isNotBlank(sortType)) {
            map.put("sortType", sortType);
        }
        if (StringUtils.isNotBlank(trackId)) {
            map.put("trackId", trackId);
        } if (StringUtils.isNotBlank(type)) {
            map.put("type", type);
        }
        map.put("pickUpNumber", pickUpNumber);
        map.put("status", status);
        map.put("current", current);
        map.put("pageSize", size);
        map.put("companyId", companyId == null ? null : companyId + "");
        return service.getListPage(map);
    }

    @UserSessionRequired
    @GetMapping("/exportExcel")
    @ApiOperation(value = "导出拣货单", notes = "导出拣货单")
    public void exportExcel(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id = request.getParameter("id");
        String sortType = request.getParameter("sortType");
        service.exportExcel(Long.valueOf(id), sortType, response);
    }

    @UserSessionRequired
    @GetMapping("/getRackStock")
    @ApiOperation(value = "拣货单编辑-获取库位下拉框", notes = "拣货单编辑-获取库位下拉框")
    @ApiResponses(@ApiResponse(code = 200, message = "", response = RackStockRes.class))
    public SingleResult getRackStock(Long id, Long productId, Long storageId) {
        return SingleResult.success(service.getRackStock(id, productId, storageId));
    }


    @UserSessionRequired
    @GetMapping("/updatePickUpInit")
    @ApiOperation(value = "拣货单编辑初始化", notes = "拣货单编辑初始化")
    @ApiResponses(@ApiResponse(code = 200, message = "", response = StockPickUpRes.class))
    public SingleResult updatePickUpInit(Long id, String sortType) {
        return SingleResult.success(service.updatePickUpInit(id, sortType));
    }


    @UserSessionRequired
    @PutMapping("/updatePickUp")
    @ApiOperation(value = "拣货单编辑确认", notes = "拣货单编辑确认")
    public SingleResult updatePickUp(@RequestBody @Validated StockPickUpdateReq req) {
        Long userId = getCurrentUserId();
        service.updatePickUp(req, userId);
        return SingleResult.success("");
    }

    @UserSessionRequired
    @GetMapping("pickUpPreview/{id}")
    @ApiOperation(value = "拣货单预览", notes = "拣货单预览")
    @ApiResponses(@ApiResponse(code = 200, message = "", response = StockPickupPreviewRes.class))
    public SingleResult pickUpPreview(@PathVariable("id") Long id) {
        return SingleResult.success("查询成功", service.pickUpPreview(id));
    }


    @UserSessionRequired
    @GetMapping("/claimPickup")
    @ApiOperation(value = "波次认领", notes = "波次认领")
    public SingleResult claimPickup(Long id, Long userId) {
        service.claimPickup(id, userId);
        return SingleResult.success();
    }



    @UserSessionRequired
    @GetMapping("/getClaimUserId")
    @ApiOperation(value = "波次认领人员列表", notes = "波次认领人员列表")
    public SingleResult getClaimUserId() {
        List<Map<String, Object>> claimUserId = service.getClaimUserId();
        return SingleResult.success(claimUserId);
    }
}

